﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using BankCharts.Silverlight.ViewModel.Models;
using System.Collections.ObjectModel;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight;

namespace BankCharts.Silverlight.ViewModel.SubViewModel
{
    public class CategoryManagerVM : ViewModelBase
    {
        #region Ctor
        public CategoryManagerVM()
        {
            PrepareCommand();
        }
        #endregion

        #region Categories
        private ObservableCollection<Category> _Categories = new ObservableCollection<Category>();

        /// <summary>
        /// Gets the Categories property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// This property's value is broadcasted by the Messenger's default instance when it changes.
        /// </summary>
        public ObservableCollection<Category> Categories
        {
            get
            {
                return _Categories;
            }

            set
            {
                if (_Categories == value)
                {
                    return;
                }

                _Categories = value;

                // Update bindings, no broadcast
                RaisePropertyChanged("Categories");
            }
        }
        #endregion

        #region SelectedCategory
        private Category _SelectedCategory = null;

        /// <summary>
        /// Gets the SelectedCategory property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// This property's value is broadcasted by the Messenger's default instance when it changes.
        /// </summary>
        public Category SelectedCategory
        {
            get
            {
                return _SelectedCategory;
            }

            set
            {
                if (_SelectedCategory == value)
                {
                    return;
                }

                _SelectedCategory = value;

                // Update bindings, no broadcast
                RaisePropertyChanged("SelectedCategory");
                AddIdentificationTokenCommand.RaiseCanExecuteChanged();
            }
        }
        #endregion

        #region Commands

        public RelayCommand<string> AddCategoryCommand { get; set; }
        public RelayCommand<string> AddIdentificationTokenCommand { get; set; }

        public void PrepareCommand()
        {
            AddCategoryCommand = new RelayCommand<string>(AddCategory);
            AddIdentificationTokenCommand = new RelayCommand<string>(AddIdentificationToken, CanAddIdentificationToken);
        }

        public void AddCategory(string categoryName)
        {
            Category c = new Category(this);
            c.Name = categoryName;
            Categories.Add(c);
            SelectedCategory = c;
        }

        public void AddIdentificationToken(string token)
        {
            if (CanAddIdentificationToken(token))
            {
                SelectedCategory.Tokens.Add(token);
            }
        }

        public void RemoveCategory(Category category)
        {
            Categories.Remove(category);
        }

        public bool CanAddIdentificationToken(string token)
        {
            return SelectedCategory != null && !string.IsNullOrWhiteSpace(token);
        }

        #endregion
    }
}
